<!--
 * @Description: 
 * @Author: mxy
 * @Date: 2022-11-15 19:36:30
 * @LastEditors: mxy
 * @LastEditTime: 2022-11-15 21:21:10
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="./css/bootstrap.min.css" />
  <style>
    .form-select {
      width: 103px;
      display: inline-block;
    }

    .col-form-label {
      text-align: right;
    }

    .figure-img {
      width: 100px;
      height: 100px;
    }

    #upload {
      display: none;
    }

    .figure-caption {
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="container">
    <h1 class="p-5">个人设置</h1>
    <form class="col-6">
      <div class="row mb-3">
        <label class="col-form-label col-3">用户名：</label>
        <div class="col-7">
          <input id="username" disabled class="form-control col-9" type="text" />
        </div>
      </div>
      <div class="row mb-3">
        <label class="col-form-label col-3">籍贯：</label>
        <div class="col-9">
          <select class="form-select col-4" name="province">
            <option value="">--省--</option>
          </select>
          <select class="form-select col-4" name="city">
            <option value="">--市--</option>
          </select>
          <select class="form-select col-4" name="county">
            <option value="">--县--</option>
          </select>
        </div>
      </div>
      <div class="row mb-3">
        <label class="col-form-label col-3">头像：</label>
        <div class="col-9">
          <figure class="figure">
            <input type="file" id="upload" />
            <img src="./imgs/default.jpg" class="figure-img img-fluid rounded" alt="..." />
            <figcaption class="figure-caption">修改头像</figcaption>
          </figure>
        </div>
      </div>
      <div class="row mb-3">
        <label class="col-3"></label>
        <div class="col-9">
          <button class="btn btn-primary">保存</button>
        </div>
      </div>
    </form>
  </div>

  <script src="./lib/bootstrap.min.js"></script>
  <script src="./lib/axios.js"></script>
  <script src="./lib/form-value.js"></script>
  <script src="./lib/request.js"></script>
  <script>
    //获取信息，渲染页面
    (async function () {
      const { data: res } = await axios.get('/user/info')
      //用户名：不用判断
      document.querySelector('#username').value = res.data.username
      console.log(res)
      //头像
      if (res.data.avatar) {
        document.querySelector('img').src = res.data.avatar
      }
      //省市县
      if (res.data.province && res.data.city && res.data.county) {
        //省是因为一开始就有，所以直接赋值
        province.value = res.data.province
        //市,县
        let p1 = axios.get('/api/city', { params: { pname: res.data.province } })
        let p2 = axios.get('/api/county', { params: { pname: res.data.province, cname: res.data.city } })
        let [{ data: shiArr }, { data: xianArr }] = await Promise.all([p1, p2])
        let arr1 = ['<option value="">--市--</option>']
        shiArr.forEach(item => arr1.push(`<option value="${item}">${item}</option>`))
        city.innerHTML = arr1.join('')
        city.value = res.data.city

        let arr2 = ['<option value="">--县--</option>']
        xianArr.forEach(item => arr2.push(`<option value="${item}">${item}</option>`))
        county.innerHTML = arr2.join('')
        county.value = res.data.county

      }


    })()

    //头像
    document.querySelector('.figure-caption').addEventListener('click', function () {
      document.querySelector('#upload').click()
    })
    document.querySelector('#upload').addEventListener('change', async function () {
      if (this.files.length > 0) {
        const fileObj = this.files[0]
        let fd = new FormData()
        fd.append('avatar', fileObj)
        const { data: res } = await axios.post('/user/avatar', fd)
        if (res.status === 0) {
          document.querySelector('img').src = res.data
        }
      }
    })

    //省市县
    const province = document.querySelector('[name=province]')
    const city = document.querySelector('[name=city]')
    const county = document.querySelector('[name=county]')
      //页面一开始要获取所有的省渲染到下拉列表中
      ; (async function () {
        const { data: res } = await axios.get('/api/province')
        const arr = ['<option value="">--省--</option>']
        res.forEach(item => arr.push(`<option value="${item}">${item}</option>`))
        province.innerHTML = arr.join('')
      })()
    //当省列表值发生改变，获取所有市
    province.addEventListener('change', async function () {
      county.innerHTML = '<option value="">--县--</option>'
      if (province.value === '') {
        city.innerHTML = '<option value="">--市--</option>'
        return
      }
      const { data: res } = await axios.get('/api/city', { params: { pname: province.value } })
      const arr = ['<option value="">--市--</option>']
      res.forEach(item => arr.push(`<option value="${item}">${item}</option>`))
      city.innerHTML = arr.join('')
    })
    //同理，当市发生改变时，获取所有的县
    city.addEventListener('change', async function () {
      if (city.value === '') {
        county.innerHTML = '<option value="">--县--</option>'
        return
      }
      const { data: res } = await axios.get('/api/county', { params: { pname: province.value, cname: city.value } })
      const arr = ['<option value="">--县--</option>']
      res.forEach(item => arr.push(`<option value="${item}">${item}</option>`))
      county.innerHTML = arr.join('')
    })


    //点击保存，提交信息
    document.querySelector('.btn-primary').addEventListener('click', async function (e) {
      e.preventDefault()
      const datas = val(document.querySelector('form'))
      const { data: res } = await axios.post('/user/info', datas)
      if (res.status === 0) {
        alert(res.message)
      }
    })
  </script>
</body>

</html>